<?php
namespace Admin\Controller;
use Think\Controller;
class AdController extends CommonController {
	//版位列表
	public function index($id = null){
		//实例化对象
		$guang_gao = M('ad');
		// 查询数据
		$gg_list = $guang_gao -> where('pid='.$id) -> order('adid ASC') -> select();
		$this->assign("id",$id);
		$this -> assign('gg_list',$gg_list);
		$this -> display();
	}

	//添加广告
	public function add($pid = null){

		if(IS_POST){
			$rules = array(
				array('adname','require','*版位名称为必填项'),//默认情况下用正则进行验证
				array('adname','','版位名称已经存在！',0,'unique',1), //adname字段唯一性验证
				array('ad_url','require','*广告链接位必填项'),
			);
			//实例化ad对象
			$ad = M('ad');
			if (!$ad->validate($rules)->create()){
			     // 如果创建失败 表示验证没有通过 输出错误提示信息
			     alertMes($ad->getError());
			}else{
				//实例化对象
				$files = M('files');
				//调用上传
			    $path = $this -> upload();
			    $data['path'] = $path;
			    $res = $files -> add($data);
			    $ad_data = $ad -> create();
			    $time = time();
			    $ad_data['addtime'] = $time;
			    $ad_data['fileid'] = $res;
			    if($res){
			    	$ad_res = $ad -> add($ad_data);
			    	if($ad_res){
			    		alertMes('添加成功',"/admin/ad/index/id/{$pid}");
			    	}else{
			    		echo "失败";
			    	}
			    }else{
			    	alertMes('请添加图片');
			    }

			}
		}else{
			//判断版位ID是否存在
			if(!empty($pid)){
				$this -> assign('pid',$pid);
				$this->display();
			}else{
				alertMes('广告版位不存在',"/admin/ad/index/id/{$id}");
			}
		}
	}

	//修改广告
	public function edit($adid = null){
		if(IS_POST){
			$rules = array(
				array('adname','require','*版位名称为必填项'),//默认情况下用正则进行验证
				array('adname','','版位名称已经存在！',0,'unique',1), //adname字段唯一性验证
				array('ad_url','require','*广告链接位必填项'),
			);
			//实例化对象
			$ad = M('ad');

			if(!$ad->validate($rules)->create()){
				// 如果创建失败 表示验证没有通过 输出错误提示信息
			    alertMes($ad->getError());
			}else{
				$fileid = I('post.fileid');
				//实例化对象
				$files = M('files');
				//判断是否有文件上传
				$x_path = $_FILES['path']['tmp_name'];

				//调用上传
				if($x_path == null){
					$path = $_POST['y_path'];
				}else{
 					$path = $this -> upload();
				}
			    $data['path'] = $path;
			    $res = $files -> where('id='.$fileid) -> save($data);
			    $arr = $ad->create();
			    //判断是否修改ad
			    	$pid = $arr['pid'];
			    	$ad_res = $ad -> save($arr);
			    	if($ad_res !== false){
			    		alertMes('修改广告成功',"/admin/ad/index/id/{$pid}");
			    	}else{
			    		alertMes('修改失败',"/admin/ad/index/id/{$pid}");
			    	}

			}

		}else{
			//判断广告ID是否为空
			if(!empty($adid)){
				// 实例化对象
				$ad = M('ad');
				$data = $ad -> getByAdid($adid);
				// 实例化files
				$files = M('files');
				$path = $files -> getById($data['fileid']);
				$this -> assign('path',$path);
				$this -> assign('data',$data);
			}else{
				// 实例化对象
				$ad = M('ad');
				$data = $ad -> getByAdid($adid);
				alertMes('请选择要修改广告',"/admin/ad/index/id/{$data['pid']}");
			}
			$this -> display();
		}
	}

	//输出广告
	public function delete($adid = null){
		//判断广告是否存在
		if(!empty($adid)){
			//获取广告的PID
			$ad = M('ad');
			$data = $ad -> getByAdid($adid);
			$fileid = $data['fileid'];
			$pid = $data['pid'];
			$files = M('files');
			$res = $files -> where('id='.$fileid) -> delete();
			// 判断图片是否删除
			if($res){
				// 删除广告
				$ad_res = $ad -> where('adid='.$adid) -> delete();
				if($ad_res){
					alertMes('广告删除成功',"/admin/ad/index/id/{$pid}");
				}else{
					alertMes('广告删除失败',"admin/ad/index/id/{$pid}");
				}
			}else{
				alertMes('图片删除失败',"admin/ad/index/id/{$pid}");
			}
		}else{
			alertMes('广告不存在，无法删除');
		}
	}
	//图片上传类
	protected function upload(){
		// 实例化上传类
	    $upload = new \Think\Upload();
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Upload/'; // 设置附件上传根目录
	    $upload->savePath  =     '/image/'; // 设置附件上传（子）目录
	    // 上传文件
	    $info = $upload->upload();
	    if(!$info) {// 上传错误提示错误信息
	        echo $upload->getError();
	    }else{// 上传成功
	    	foreach($info as $file){
	            return $path=__ROOT__.'/Upload/'.$file['savepath'].$file['savename'];
	        }
	    }
	}

}
